package com.example.demo.leetcode.classics150;

/**
 * ******************************************************
 *
 * @author liugh9
 * @version 1.0
 * @classname _88验证二叉搜索树
 * @description
 * @date 2023/08/06 9:46
 * <p>
 * ******************************************************
 */
public class _88验证二叉搜索树 {

    public boolean isValidBST(TreeNode root) {
        return validBST(root, null, null);
    }

    private boolean validBST(TreeNode root, TreeNode max, TreeNode min) {
        if (root == null) {
            return true;
        }
        if (max != null && max.val <= root.val) return false;
        if (min != null && min.val >= root.val) return false;
        return validBST(root.left, root, min) && validBST(root.right, max, root);
    }

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
